package br.com.emanuelvictor.architectureandroid.app1.entity;

import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;

import br.com.emanuelvictor.architectureandroid.app1.repository.PointRepository;

public class Daily {
	private PointRepository repository;
	private List<Point> points = new LinkedList<Point>();
	private long morningDifference = 0;
	private long afternoonDifference = 0;
	private long dayDifference = 0;
	
	public Daily() {	}
	
	public Daily(PointRepository repository, Calendar calendar) {
		this.repository = repository;
		
		this.init(calendar);
	}
	
	public void setPoints(List<Point> points){
		this.points = points;
	}
	private void init(Calendar calendar) {
		
		points = repository.findAllByDate(calendar);
		
		if (points != null && points.size() > 0) {
			if (isHalfDay() || isFullDay()) {
				morningDifference = (points.get(1).date.getTimeInMillis() - points.get(0).date.getTimeInMillis()) - (28800000 / 2);
			}
			if (isFullDay()) {
				afternoonDifference = (points.get(3).date.getTimeInMillis() - points.get(2).date.getTimeInMillis()) - (28800000 / 2);
				dayDifference = ((points.get(1).date.getTimeInMillis() - points.get(0).date.getTimeInMillis()) + (points.get(3).date.getTimeInMillis() - points.get(2).date.getTimeInMillis())) - 28800000;
			}
		}
	}
	
	public Calendar getMorningFirstHour() {
		if (points.size() >= 1) {
			return points.get(0).date;
		}
		return null;
	}
	
	public Calendar getMorningSecondHour() {
		if (points.size() >= 2) {
			return points.get(1).date;
		}
		return null;
	}
	
	public Calendar getAfternoonFirstHour() {
		if (points.size() >= 3) {
			return points.get(2).date;
		}
		return null;
	}
	
	public Calendar getAfternoonSecondHour() {
		if (points.size() == 4) {
			return points.get(3).date;
		}
		return null;
	}
	
	public long getMorningDifference() {
		return this.morningDifference;
	}
	
	public long getAfternoonDifference() {
		return this.afternoonDifference;
	}
	
	public long getDayDifference() {
		return this.dayDifference;
	}
	
	public List<Point> getPoints() {
		return this.points;
	}
	
	public boolean isHalfDay() {
		return (points.size() >= 2 && points.size() < 4);
	}
	
	public boolean isFullDay() {
		return (!isHalfDay() && points.size() == 4);
	}
}

